package com.rntemplate;

import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import androidx.core.app.ActivityCompat;

import com.facebook.react.ReactActivity;
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.ReactRootView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends ReactActivity {

  private final int REQUEST_PERMISSION_CODE = 1001;

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initPermission();
    }

  /**
   * Returns the name of the main component registered from JavaScript. This is used to schedule
   * rendering of the component.
   */
  @Override
  protected String getMainComponentName() {
    return "RnTemplate";
  }

  /**
   * Returns the instance of the {@link ReactActivityDelegate}. There the RootView is created and
   * you can specify the rendered you wish to use (Fabric or the older renderer).
   */
  @Override
  protected ReactActivityDelegate createReactActivityDelegate() {
    return new MainActivityDelegate(this, getMainComponentName());
  }

  private void initPermission() {
      List<String> mPermissionList = new ArrayList<>();
      // Android 版本 >= 12 时，配置申请新的蓝牙权限
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
          mPermissionList.add(Manifest.permission.BLUETOOTH_SCAN);
          mPermissionList.add(Manifest.permission.BLUETOOTH_ADVERTISE);
          mPermissionList.add(Manifest.permission.BLUETOOTH_CONNECT);
      } else {
          mPermissionList.add(Manifest.permission.ACCESS_COARSE_LOCATION);
          mPermissionList.add(Manifest.permission.ACCESS_FINE_LOCATION);
      }
      ActivityCompat.requestPermissions(this, mPermissionList.toArray(new String[0]), REQUEST_PERMISSION_CODE);
  }

  public static class MainActivityDelegate extends ReactActivityDelegate {
    public MainActivityDelegate(ReactActivity activity, String mainComponentName) {
      super(activity, mainComponentName);
    }

    @Override
    protected ReactRootView createRootView() {
      ReactRootView reactRootView = new ReactRootView(getContext());
      // If you opted-in for the New Architecture, we enable the Fabric Renderer.
      reactRootView.setIsFabric(BuildConfig.IS_NEW_ARCHITECTURE_ENABLED);
      return reactRootView;
    }
  }
}
